//
//  SearchViewController.m
//  xhtios1.0
//
//  Created by hlt on 15/4/30.
//  Copyright (c) 2015年 hlt. All rights reserved.
//

#import "SearchViewController.h"
#import "HJYSearchHistoryView.h"
#import "HLTsearchlistViewController.h"
@interface SearchViewController ()<HJYSearchHistoryViewDelegate>
{
    HJYSearchHistoryView       *_recentSearchView;
    UIView                     *_backGroundView;
    
    UISearchBar                *seach;
    
}
@end

@implementation SearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
//       self.view.backgroundColor=navigationBarColor;
    //设置导航条的背景颜色和字体颜色
//    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"01登录界面2_r2_c1.gif"] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.barTintColor = navigationBarColor;
    self.navigationController.navigationBar.tintColor=[UIColor whiteColor];
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor,nil]];
    [self.navigationItem setTitle:@""];
//    self.title=@"";
    _searchbar.delegate = self;
    _searchbar.showsCancelButton = NO;
    //去除背景色
    for (UIView *view in _searchbar.subviews) {
        // for before iOS7.0
        if ([view isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
            [view removeFromSuperview];
            break;
        }
        // for later iOS7.0(include)
        if ([view isKindOfClass:NSClassFromString(@"UIView")] && view.subviews.count > 0) {
            [[view.subviews objectAtIndex:0] removeFromSuperview];
            break;
        }
    }
    
    NSMutableArray *leftItems = [NSMutableArray array];
    
    
    
    
    // 返回按钮
    UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [searchBtn setTitle:@"搜索" forState:UIControlStateNormal];
    //    [backBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    searchBtn.frame = CGRectMake(0, 0, 50, 30);
    [searchBtn addTarget:self action:@selector(searchBarSearchButton) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backBtnItem = [[UIBarButtonItem alloc] initWithCustomView:searchBtn];
    // 添加
    self.navigationItem.rightBarButtonItem = backBtnItem;
    
    CGFloat backBtnWidth = 0;
    if (self.sourceType == SearchBarTypeHome) {
        UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [backBtn setTitle:@"返回" forState:UIControlStateNormal];
        //    [backBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        backBtn.frame = CGRectMake(0, 0, 40, 30);
        [backBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *backBtnItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
        [leftItems addObject:backBtnItem];
        backBtnWidth = 50;
    }
    
    
    // 搜索框
    UISearchBar *searchBar = [[UISearchBar alloc] init];
    NSLog(@"%f",screen_width);
    searchBar.bounds = CGRectMake(0, 0, screen_width-80-backBtnWidth-10, 30);
    searchBar.delegate = self;
    searchBar.placeholder = @"请输入搜索关键词";
    searchBar.tintColor=[UIColor blackColor];
    [searchBar becomeFirstResponder];
    
    UIBarButtonItem *searchBarItem = [[UIBarButtonItem alloc] initWithCustomView:searchBar];
    [leftItems addObject:searchBarItem];
    
    // 添加
    self.navigationItem.leftBarButtonItems = leftItems;
    
    seach=searchBar;
    
    
    
    
    
    
    
    
    //设置搜索取消按钮
    //    for(id cc in [_searchbar.subviews[0] subviews])
    //    {
    //        if([cc isKindOfClass:[UIButton class]])
    //        {
    //            UIButton *btn = (UIButton *)cc;
    //            [btn setTitle:@"取消" forState:UIControlStateNormal];
    //            //[btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    //        }
    //    }
    
    //添加搜索历史
    UIView *backGroundView = [[UIView alloc] init];
    backGroundView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:backGroundView];
    
    UITapGestureRecognizer *tab = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cancelEdit)];
    [backGroundView addGestureRecognizer:tab];
    
    
    
    
    CGFloat backGroundViewW = screen_width;
    CGFloat backGroundViewH = screen_height - 64-49;
    CGFloat backGroundViewX = 0;
    CGFloat backGroundViewY = -backGroundViewH;
    backGroundView.frame = CGRectMake(backGroundViewX, backGroundViewY, backGroundViewW, backGroundViewH);
    
    [UIView animateWithDuration:0.2 animations:^{
//        if (self.sourceType==SearchBarTypeHome) {
            backGroundView.frame = CGRectMake(backGroundViewX, 64, backGroundViewW, backGroundViewH);
            
//        }
//        else
//        {
//            backGroundView.frame = CGRectMake(backGroundViewX, 64, backGroundViewW, backGroundViewH);
//            
//        }
        
    } completion:^(BOOL finished) {
        
        // 最近搜索
        _recentSearchView = [[HJYSearchHistoryView alloc] initWithFrame:CGRectMake(0, 0, screen_width, screen_height - 64-49)];
        _recentSearchView.delegate = self;
        [backGroundView addSubview:_recentSearchView];
        
        
    }];
    
    _backGroundView = backGroundView;
    
    
}

-(void)backClick
{
    [self.navigationController popViewControllerAnimated:NO];
}

-(void)cancelEdit
{
    [seach resignFirstResponder];
}

#pragma mark - 自定义代理方法
/**
 在搜索页面点击最近搜索的历史记录就会调用
 */
- (void)searchHistoryView:(HJYSearchHistoryView *)searchHistoryView searchText:(NSString *)searchText
{
     NSString *str1= [searchText stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    
    HLTsearchlistViewController *shopvc=[[HLTsearchlistViewController alloc]init];
    shopvc.keyname=str1;
    shopvc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:shopvc animated:YES];
    
}




- (IBAction)nokeyboard:(id)sender {
    
    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
    
}




#pragma mark seachbar的委托方法




//点击搜索按钮时调用
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    //去掉左右两边的空格
    NSString *str1= [searchBar.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    
    _recentSearchView.searchString = str1;
    
    [self searchHistoryView:nil searchText:searchBar.text];
}


//点击搜索按钮时调用
- (void)searchBarSearchButton
{
    
    NSString *str1= [seach.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    _recentSearchView.searchString = str1;
    
    [self searchHistoryView:nil searchText:seach.text];
}






//点击取消按钮的事件
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
    
    
}









- (IBAction)search1:(UIButton *)sender {
    _recentSearchView.searchString = _searchbar.text;
    
    [self searchHistoryView:nil searchText:_searchbar.text];
    
    
    
}




-(void)viewWillAppear:(BOOL)animated
{
    
    self.navigationController.navigationBarHidden=NO;
    
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
